#include "myselfwidget.h"
#include "ui_myselfwidget.h"
#include "videobox.h"
#include "util.h"
#include "modifymyselfdialog.h"
#include <QFileDialog>
#include "video_playback.h"
MyselfWidget::MyselfWidget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::MyselfWidget)
{
    ui->setupUi(this);

    initUI();
}

MyselfWidget::~MyselfWidget()
{
    delete ui;
}

void MyselfWidget::initUI()
{
    ui->attentionBtn->hide();

#ifdef TEST_UI
    // 往视频显示区域添加VideoBox
    for(int i = 0; i < 16; i++)
    {
        VideoBox* videoBox = new VideoBox();
        ui->layout->addWidget(videoBox, i / 4, i % 4);
    }
#endif

    connect(ui->avatarBtn, &QPushButton::clicked, this, &MyselfWidget::onAvatarBtnClicked);
    connect(ui->settingBtn, &QPushButton::clicked, this, &MyselfWidget::settingBtnClicked);
    connect(ui->uploadVideoBtn, &QPushButton::clicked, this, &MyselfWidget::onUploadVideoBtnClicked);

}

void MyselfWidget::onAvatarBtnClicked()
{
    // 弹出对话框，从本地磁盘选择用户头像--QFileDialog::getOpenFileName
    QString fileName = QFileDialog::getOpenFileName(nullptr, "修改图像", "", "Image Files (*.jpg *.png)");
    if(fileName.isEmpty())
    {
        LOG() << "取消选择头像";
        return;
    }
    // 获取图片文件数据
    QByteArray fileData = loadFileToByteArray(fileName);
    if (fileData.isEmpty())
    {
        LOG() << "头像⽂件读取失败";
        return;
    }
    // 将图片数据设置成按钮图标
    ui->avatarBtn->setIcon(makeCircleIcon(fileData, ui->avatarBtn->width()/2));

    // 将图片数据上传到服务器--todo
}

void MyselfWidget::settingBtnClicked()
{
    // 弹出对话框设置用户信息
    ModifyMyselfDialog* dialog = new ModifyMyselfDialog();
    dialog->exec();
    delete dialog;
}

void MyselfWidget::onUploadVideoBtnClicked()
{
    // 弹出对话框，从本地磁盘选择用户要上传的文件--QFileDialog::getOpenFileName
    QString fileName = QFileDialog::getOpenFileName(nullptr, "上传视频", "", tr("Videos (*.mp4 *.rmvb *.avi *.mov)"));
    if(fileName.isEmpty())
    {
        LOG() << "取消上传视频";
        return;
    }

    // 检测上传视频的大小，上传视频大小不能大于4G
    QFileInfo fileInfo(fileName);
    int64_t fileSize = fileInfo.size();
    if(fileSize > (int64_t)1024*1024*1024*4)
    {
        LOG() << "视频大小必须小于4G";
        return;
    }

    // 切换到上传视频页面
    emit switchUploadVideoPage(UploadPage);
}
















